package top.lightHearted.socket;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Lazy
@Configuration
@EnableWebSocketMessageBroker
public class StompConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Value("${jms.stomp.host}")
    private String host;

    @Value("${jms.stomp.port}")
    private int port;

    @Value("${jms.username}")
    private String username;

    @Value("${jms.password}")
    private String password;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stompHandler").setAllowedOrigins("*").withSockJS().setHeartbeatTime(10000);
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay("/queue","/topic")
                .setRelayHost(host).setRelayPort(port)
                .setSystemLogin(username).setSystemPasscode(password)
                .setClientLogin(username).setClientPasscode(password)
                .setSystemHeartbeatReceiveInterval(10000).setSystemHeartbeatSendInterval(10000);
        registry.setApplicationDestinationPrefixes("/app");
        registry.setPathMatcher(new AntPathMatcher("."));
    }
}
